<?php
include ("conn.php");
$skey = $_POST['skey'];
$pid = $_POST['pid'];
$mode = $_POST['mode'];
$bigTitle = $_POST['bigTitle'];
$bigTitle = str_replace("'","''",$bigTitle);
$pageClass = $_POST['pageClass'];
$defaultPo = (int)$_POST['defaultPo'];
$parts = json_decode($_POST['parts']);
$teacherInfo = $_POST['teacherInfo'];
$teacherInfo = str_replace("'","''",$teacherInfo);
//验证skey是否存在
$sql="SELECT * FROM user WHERE thirdSession='".$skey."'";
$result = $conn->query($sql);
if ($result->num_rows == 0) {exit('{"code":450}');}
$row = $result->fetch_assoc();
$uid = $row['id'];
//解析parts
$HTMLs = [];$titles = [];$pointNum=count($parts);
for ($i=0; $i < count($parts); $i++) {
	array_push($titles,$parts[$i]->title);
	array_push($HTMLs,$parts[$i]->content);
}
$HTMLs = implode("#################",$HTMLs);
$HTMLs = str_replace("'","''",$HTMLs);
$titles = implode("#################",$titles);
$titles = str_replace("'","''",$titles);
if((int)$mode == 1){
	$sql="INSERT INTO pageList (pointNum,HTMLs,titles,title,class,time,teachers,likeNum,auid,type,defaultShow)
VALUES (".$pointNum.",'".$HTMLs."','".$titles."','".$bigTitle."','".$pageClass."','".date("Y-m-d H:i:s")."','".$teacherInfo."',0,".$uid.",0,".$defaultPo.")";
	$result = $conn->query($sql);
	echo '{"code":"ok1"}';	
}else if((int)$mode == 2){
	$sql ="UPDATE pageList 
SET pointNum=".$pointNum.", HTMLs='".$HTMLs."', titles='".$titles."', title='".$bigTitle."', class='".$pageClass."', time='".date("Y-m-d H:i:s")."', teachers='".$teacherInfo."', type=1, defaultShow=".$defaultPo."
WHERE id=".$pid;
	$result = $conn->query($sql);
	echo '{"code":"ok2"}';
}else if((int)$mode == 3){
	$sql ="UPDATE pageList 
SET pointNum=".$pointNum.", HTMLs='".$HTMLs."', titles='".$titles."', title='".$bigTitle."', class='".$pageClass."', time='".date("Y-m-d H:i:s")."', teachers='".$teacherInfo."', type=0, defaultShow=".$defaultPo."
WHERE id=".$pid;
	$result = $conn->query($sql);
	echo '{"code":"ok3"}';
}else if((int)$mode == 4){
	$sql="INSERT INTO pageList (pointNum,HTMLs,titles,title,class,time,teachers,likeNum,auid,type,defaultShow)
VALUES (".$pointNum.",'".$HTMLs."','".$titles."','".$bigTitle."','".$pageClass."','".date("Y-m-d H:i:s")."','".$teacherInfo."',0,".$uid.",1,".$defaultPo.")";
	$result = $conn->query($sql);
	echo '{"code":"ok4"}';
}
?>